package main

import (
	"time"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/dao"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context/contextutil"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/util/timer"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"github.com/spf13/cobra"
)

func NewWorkerClearObjectCommand() *cobra.Command {
	var command = &cobra.Command{
		Use:  "clear-object",
		Args: cobra.NoArgs,
		Run: func(cmd *cobra.Command, args []string) {
			defer func() {
				cmd.Println("对象清理任务已停止")
			}()

			cmd.Println("对象清理任务已启动")

			var ctx = contextutil.NewContextWithStandard(cmd.Context())

			timer.NewMinuteTicker(ctx, func(ctx context.Context, ticker *time.Ticker, now time.Time) {
				cmd.Printf("运行时间: %v\n", now)

				var ss = NewStorageService()

				{
					// 处理失效对象，标记为预删除

					var page = 1
					for {
						_, data, err := ss.ListObjectByCondition(ctx, page, 100, dao.SystemObjectOptionWithStatus(model.SystemObjectStatusUploaded), dao.SystemObjectOptionWithCreateTimeLessThan(now.Add(-time.Hour*24).Unix()), dao.SystemObjectOptionWithReferCntLessThan(0), dao.SystemObjectOptionWithOrderDefault())
						if err != nil {
							logger.SugarLogger(logger.WithStack()).Errorf("CMC_ND.WCOC_ND.381701 查询对象列表失败: %v", err)

							break
						}

						for _, v := range data {
							cmd.Printf("待标记对象ID: %v, 原始文件名称: %v, 路径: %v, 大小: %v, MD5哈希值: %v, SHA1哈希值: %v, SHA256哈希值: %v\n", v.Id, *v.OriginalFilename, *v.Filepath, *v.Size, *v.Md5, *v.Sha1, *v.Sha256)

							if err := ss.PreDeleteObject(ctx, v); err != nil {
								cmd.Printf("预删除对象失败: %v\n", err)
								logger.SugarLogger(logger.WithStack()).Errorf("CMC_ND.WCOC_ND.471651 预删除对象失败: %v", err)

								continue
							}

							cmd.Printf("已标记对象ID: %v, 原始文件名称: %v, 路径: %v, 大小: %v, MD5哈希值: %v, SHA1哈希值: %v, SHA256哈希值: %v\n", v.Id, *v.OriginalFilename, *v.Filepath, *v.Size, *v.Md5, *v.Sha1, *v.Sha256)
						}

						if 100 > len(data) {
							break
						}

						page++
					}
				}

				{
					// 处理预删除对象，删除对象

					var page = 1
					for {
						_, data, err := ss.ListObjectByCondition(ctx, page, 100, dao.SystemObjectOptionWithStatus(model.SystemObjectStatusPreDelete), dao.SystemObjectOptionWithPlanDeleteTimeLessThan(now.Unix()), dao.SystemObjectOptionWithOrderDefault())
						if err != nil {
							logger.SugarLogger(logger.WithStack()).Errorf("CMC_ND.WCOC_ND.691849 查询对象列表失败: %v", err)
						}

						for _, v := range data {
							cmd.Printf("待清理对象ID: %v, 原始文件名称: %v, 路径: %v, 大小: %v, MD5哈希值: %v, SHA1哈希值: %v, SHA256哈希值: %v\n", v.Id, *v.OriginalFilename, *v.Filepath, *v.Size, *v.Md5, *v.Sha1, *v.Sha256)

							if err := ss.DeleteObject(ctx, v); err != nil {
								cmd.Printf("删除对象失败: %v\n", err)
								logger.SugarLogger(logger.WithStack()).Errorf("CMC_ND.WCOC_ND.761854 删除对象失败: %v", err)

								continue
							}

							cmd.Printf("已清理对象ID: %v, 原始文件名称: %v, 路径: %v, 大小: %v, MD5哈希值: %v, SHA1哈希值: %v, SHA256哈希值: %v\n", v.Id, *v.OriginalFilename, *v.Filepath, *v.Size, *v.Md5, *v.Sha1, *v.Sha256)
						}

						if 100 > len(data) {
							break
						}

						page++
					}
				}
			})
		},
	}

	return command
}
